<!DOCTYPE html>
html
  head
    title login
    link(href="public/main.css" rel="stylesheet")
    
  body
    .login-page
      .form
        #error
        if strategies.local
          form.login-form
            input(id='email', type='text', name="email", placeholder='email')
            input(id='password' type='password', name="password", placeholder='password')
            button(onclick="submitForm(event)") login
        if strategies.google
          a(href="/auth/google")
            input#googleButton(type="image" src="public/assets/btn_google_signin_dark_normal_web@2x.png")

  script.
    function submitForm(event){
      event.preventDefault();
      
      const email = document.getElementById("email").value;
      const password = document.getElementById("password").value;
      
      if(email === '' || password === ''){
        document.getElementById("error").innerHTML = "Please provide email and password";
      }else{
        const req = new XMLHttpRequest();

        req.onreadystatechange = function(event) {
          // XMLHttpRequest.DONE === 4
          if (this.readyState === XMLHttpRequest.DONE) {
            if (this.status === 200) {
              if (typeof this.responseURL == 'string'){
                window.location = this.responseURL
              }
            } else {
                document.getElementById("error").innerHTML = JSON.parse(this.response).error;
            }
          }
        };

        req.open('POST', '/auth/local/login', true);
        req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        req.send('email='+email+'&password='+password);
      }
    }
